var BTN_CLS = "sexybutton";
var MAN_CLS = "mandatory";
var MAN_HIGHLITE = '*';
var isDebug = 'isDebug';
var beforeSendXHR = 'beforeSendXHR';
var beforeSendDATA = 'beforeSendData';

var forwardTo = function(url, data, mode, formName) {
	var $form = $("#submitForm");
	if (formName) {
		$form = $("#" + formName);
	}
	$form.empty();
	$form.attr("method", "post").attr("action", url).append(
			$("<input>").attr("name", "mode").val(mode));
	if (data) {
		for ( var key in data) {
			var fldName = key;
			var fldValue = data[key];
			$form.append($("<input>").attr("name", fldName).val(fldValue));
			// $form.append( $("<input>").attr("name", "mode").val("forward") );
		}
	}
	$form.submit();
};

var misoap = function(url, tmplUrl, data, successFunc, errorFunc, isSoap11) {
	var isDbg = miShare(isDebug);
	if (isDbg) {
		console.log('template-url:' + tmplUrl);
		console.log('template-data:');
		console.log(data);
	}
	var soapMessage = new EJS({
		url : tmplUrl
	}).render(data);
	var contentType = "text/xml; charset=\"utf-8\"";
	if (!isSoap11) {
		contentType = "application/soap+xml; charset=\"utf-8\"";
	}
	if (isDbg) {
		console.log('url:' + url);
		console.log('Content-Type:' + contentType);
		console.log('Sending:' + soapMessage);
	}
	var requestInfo = {};
	var beforeSendFunc = function(xhr, data) {
		requestInfo = data;
		if (isDbg) {
			console.log('xhr:');
			console.log(xhr);
			console.log('settings:');
			console.log(data);
		}
		miShare(beforeSendXHR, xhr);
		miShare(beforeSendDATA, data);
	};

	var ajax = {
		url : url,
		type : "POST",
		dataType : "xml",
		data : soapMessage,
		complete : successFunc,
		error : errorFunc,
		contentType : contentType,
		beforeSend : beforeSendFunc
	};
	$.ajax(ajax);
	// $.ajax(ajax).done(successFunc).fail(errorFunc);
	return soapMessage;
};

var mijax = function(action, url, data, successFunc, errorFunc, isAsync,
		timeOutVal, dataType) {
	if (!dataType) { // when it is not define
		dataType = 'json'; // default to json datatype.
	}
	var isDbg = miShare(isDebug);

	if (!errorFunc) { // when it is not define
		errorFunc = function(xhr, textStatus) {
			if (xhr && xhr.status == 200) {
				return;
			}
			if (isDbg) {
				console.log("While accessing: " + url
						+ "\nAn error occured.\nstatus code:" + xhr.status
						+ ";\nmessage:\n" + xhr.statusText);
				console.log('xhr:\n');
				console.log(xhr);
				console.log('textStatus:\n');
				console.log(textStatus);
			}
		};
	}
	// DO NOT use async in a page with multiple call. Use sync call whenever
	// possible. this will avoid hanging the browser being executed too fast
	// when there is
	// multiple calls.
	var isAsyncCall = false;
	if (isAsync) {
		// ### 2012-Oct-17, bp.ong: new isAsyncCall is
		// true to avoid UI freeze.
		isAsyncCall = isAsync;
	}
	var timeOutDefault = 15000; // 15 seconds.
	// ### 2012-Oct-17, bp.ong: new timeout parameter is added to avoid UI
	// freeze.
	if (timeOutVal) {
		timeOutDefault = timeOutVal;
	}

	var requestInfo = {};
	var beforeSendFunc = function(xhr, data) {
		requestInfo = data;
		if (isDbg) {
			console.log('xhr:');
			console.log(xhr);
			console.log('settings:');
			console.log(data);
		}
		miShare(beforeSendXHR, xhr);
		miShare(beforeSendDATA, data);
	};

	var contentType = "application/json; charset=\"utf-8\"";
	var sendData = JSON.stringify(data);
	if (dataType === 'xml') {
		contentType = "application/xml; charset=utf-8";
	}
	var ajax = {
		type : action,
		url : url,
		dataType : dataType,
		contentType : contentType,
		data : sendData,
		beforeSend : beforeSendFunc,
//		complete : successFunc, 
//		error : errorFunc,
		async : isAsyncCall,
		timeout : timeOutDefault
	};// end of ajax.
	
	// $.ajax(ajax);
	$.ajax(ajax).done(successFunc).fail(errorFunc); 
	return requestInfo;
};

var posAjax = function(url, data, successFunc, errorFunc, timeOutVal, dataType) {
	return mijax("POST", url, data, successFunc, errorFunc, true, timeOutVal,
			dataType);
};

var getAjax = function(url, data, successFunc, errorFunc, timeOutVal, dataType) {
	return mijax("GET", url, data, successFunc, errorFunc, true, timeOutVal,
			dataType);
};

var putAjax = function(url, data, successFunc, errorFunc, timeOutVal, dataType) {
	return mijax("PUT", url, data, successFunc, errorFunc, true, timeOutVal,
			dataType);
};

var delAjax = function(url, data, successFunc, errorFunc, timeOutVal, dataType) {
	return mijax("DELETE", url, data, successFunc, errorFunc, true, timeOutVal,
			dataType);
};

var posjax = function(url, data, successFunc, errorFunc, isAsync, timeOutVal,
		dataType) {
	return mijax("POST", url, data, successFunc, errorFunc, isAsync,
			timeOutVal, dataType);
};

var getjax = function(url, data, successFunc, errorFunc, isAsync, timeOutVal,
		dataType) {
	return mijax("GET", url, data, successFunc, errorFunc, isAsync, timeOutVal,
			dataType);
};

var putjax = function(url, data, successFunc, errorFunc, isAsync, timeOutVal,
		dataType) {
	return mijax("PUT", url, data, successFunc, errorFunc, isAsync, timeOutVal,
			dataType);
};

var deljax = function(url, data, successFunc, errorFunc, isAsync, timeOutVal,
		dataType) {
	return mijax("DELETE", url, data, successFunc, errorFunc, isAsync,
			timeOutVal, dataType);
};

var miShare = function(name, valFunc) {
	if (valFunc) {
		$('head').data(name, valFunc);
	} else {
		return $('head').data(name);
	}
};

var chkEmpty = function(responseObj, emptyMessage, notEmptyMessage, retVal) {
	if (responseObj == null) {
		if (emptyMessage) {
			alert(emptyMessage);
		}
		return retVal;
	}
	var responseText = JSON.parse(responseObj.responseText);
	if (responseText == null || responseText.trim() == '') {
		if (emptyMessage) {
			alert(emptyMessage);
		}
		return retVal;
	} else {
		if (notEmptyMessage) {
			alert(notEmptyMessage);
		}
		return !retVal;
	}
};

var isNotEmpty = function(responseObj, notEmptyMessage, emptyMessage) {
	return chkEmpty(responseObj, emptyMessage, notEmptyMessage, false);
};

var isEmpty = function(responseObj, emptyMessage, notEmptyMessage) {
	return chkEmpty(responseObj, emptyMessage, notEmptyMessage, true);
};

var hasText = function(responseObj, text, okMessage, failMessage) {
	if (responseObj == null) {
		if (failMessage) {
			alert(failMessage);
		}
		return false;
	}
	var obj = JSON.parse(responseObj.responseText);
	if (obj == null) {
		if (failMessage) {
			alert(failMessage);
		}
		return false;
	}
	// alert('Status:'+obj.status);
	if (obj.status === text || checkRE(obj.status, text)) {
		if (okMessage) {
			alert(okMessage);
		}
		return true;
	} else {
		if (failMessage) {
			alert(failMessage);
		}
		return false;
	}
};

var isSuccess = function(responseObj, okMessage, failMessage) {
	if (responseObj == null) {
		if (failMessage) {
			alert(failMessage);
		}
		return false;
	}
	var obj = JSON.parse(responseObj.responseText);
	if (obj == null) {
		if (failMessage) {
			alert(failMessage);
		}
		return false;
	}
	if (obj.status === "succeed") {
		if (okMessage) {
			alert(okMessage);
		}
		return true;
	} else {
		if (failMessage) {
			alert(failMessage);
		}
		return false;
	}
};

var isFailure = function(responseObj, failMessage, okMessage) {
	if (responseObj == null) {
		if (failMessage) {
			alert(failMessage);
		}
		return true;
	}
	var obj = JSON.parse(responseObj.responseText);
	if (obj == null) {
		if (failMessage) {
			alert(failMessage);
		}
		return true;
	}
	if (obj.status === "succeed") {
		if (okMessage) {
			alert(okMessage);
		}
		return false;
	} else {
		if (failMessage) {
			alert(failMessage);
		}
		return false;
	}
};

var mkButton = function(add2Tag, actClass, text) {
	var pfxName = 'btn' + actClass;
	var $tag = $('<button>').addClass(BTN_CLS);
	$tag.attr('type', 'button');
	$tag.attr('id', pfxName);
	var $span1 = $('<span>');
	var $span2 = $('<span>');
	var $span3 = $('<span>');
	$span3.addClass(actClass);
	$span3.text(text);
	$tag.append($span1.append($span2.append($span3)));
	if (add2Tag) {
		$('#' + add2Tag).append($tag);
	}
	return $tag;
};
